﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Musicman.Business;
using Musicman.Common.Entities;

namespace MusicmanWeb.ADMIN
{
    public partial class UserStats : MusicmanPage
    {
        override protected void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            long personId = -1;
            if (Int64.TryParse(Request["PersonID"], out personId))
            {
                BindUser(personId);
            }  
        }

        private void BindUser(long userId)
        {
            Musicman.Common.Entities.User user = UserManager.Instance.GetUser(userId, true);
            this.profilepicture.Src = String.Format("ProfilePicture.axd?personId={0}", userId);
            this.firstnameLbl.InnerText = user.Firstname;
            this.lastnameLbl.InnerText = user.Lastname;
            this.emailLbl.InnerText = user.Emailaddress;
            this.sexLbl.InnerText = user.PersonSex.ToString();
            this.mailRqLbl.InnerText = user.MailRequest.ToString();
            string userStatus = "";
            if (user.Disabled)
            {
                userStatus = "disabled";
            }
            else if (user.UserStats.LastLoggedDate < DateTime.Now.AddDays(-14))
            {
                userStatus = "very idle";
            }
            else if (user.UserStats.LastLoggedDate < DateTime.Now.AddDays(-7))
            {
                userStatus = "idle";
            }
            else
            {
                userStatus = "active";
            }
            this.statusLbl.InnerText = userStatus;
            this.lastLoggedLbl.InnerText = user.UserStats.LastLoggedDate.ToString();
            this.totalWebLbl.InnerText = user.UserStats.TotalLogsWeb.ToString();
            this.totalClientLbl.InnerText = user.UserStats.TotalLogsCient.ToString();
            this.totalDownloadsLbl.InnerText = user.UserStats.TotalFilesDownloaded.ToString();
            string totalTraffic = "";
            if (user.UserStats.TotalFilesLength > 1073741824)
            {
                totalTraffic = Musicman.Common.Utilities.Converter.LengthGB(user.UserStats.TotalFilesLength);
            }
            else
            {
                totalTraffic = Musicman.Common.Utilities.Converter.LengthMB(user.UserStats.TotalFilesLength);
            }
            this.totalTrafficLbl.InnerText = totalTraffic;
            toggleEnabledLB.Text = user.Disabled ? "enable" : "disable";
            toggleEnabledLB.CommandName = user.Disabled ? "enable" : "disable";
            toggleEnabledLB.CommandArgument = user.Personid.ToString();
            userhistorylink.Attributes["onclick"] = String.Format("window.open('../USER/UserHistory.aspx?personid={0}', 'userhistory', ',width=500,height=440,menubar=no,breplace=yes').focus();", user.Personid);
            deleteUserLB.CommandArgument = user.Personid.ToString();
        }


        protected void ToggleEnabled_Command(object sender, CommandEventArgs e)
        {
            bool enable = e.CommandName == "enable";
            long personId = Int64.Parse(e.CommandArgument.ToString());
            if (UserManager.Instance.ToggleEnabled(personId, enable))
            {
                List<User> users = Cache["users"] as List<User>;
                if (users != null)
                {
                    User user = users.Find(delegate(User u) { return u.Personid == personId; });
                    if (user != null)
                    {
                        user.Disabled = !enable;
                    }
                }
            }
            BindUser(personId);
        }

        protected void DeleteUser_Command(object sender, CommandEventArgs e)
        {
            long personId = Int64.Parse(e.CommandArgument.ToString());
            if (UserManager.Instance.DeleteUser(personId))
            {
                List<User> users = Cache["users"] as List<User>;
                if (users != null)
                {
                    User user = users.Find(delegate(User u) { return u.Personid == personId; });
                    users.Remove(user);
                }
            }
        }
    }
}
